package cst.a931xmc.yuejian20.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageButton;

import cst.a931xmc.yuejian20.Fragment.Me.MyAorderFragment;
import cst.a931xmc.yuejian20.Fragment.Me.MyTorderFragment;
import cst.a931xmc.yuejian20.R;
import cst.a931xmc.yuejian20.Widget.PagerSlidingTabStrip;

public class MyOrderActivity extends AppCompatActivity implements View.OnClickListener {
    private MyAorderFragment aorderFragment;
    private MyTorderFragment torderFragment;
    private ImageButton btn_back;
    private PagerSlidingTabStrip tabs;
    /**
     * 获取当前屏幕的密度
     */
    private DisplayMetrics dm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myorder);
        init();
    }

    private void init() {
        dm = getResources().getDisplayMetrics();
        ViewPager pager = findViewById(R.id.pager);
        tabs = findViewById(R.id.tabs);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
    }

    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#FF5B5B"));// #45c01a
        tabs.setTextColor(Color.parseColor("#797979"));
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTextColor(Color.parseColor("#EE3232"));// #45c01a
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = { "活动订单", "培训订单" };

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (null == aorderFragment) {
                        aorderFragment = new MyAorderFragment();
                    }
                    return aorderFragment;
                case 1:
                    if (null == torderFragment) {
                        torderFragment = new MyTorderFragment();
                    }
                    return torderFragment;
                default:
                    return null;
            }
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }
}